#include "CRL_AtomicFactory.h"
#include "OpenGL/COGL_AtomicFactory.h"
#include "DirectX9/CDX9_AtomicFactory.h"
#include "Directx10/CDX10_AtomicFactory.h"
#include "Directx11/CDX11_AtomicFactory.h"

CB_SUBNAMESPACE_START(RenderLib)

CAtomicFactory* CAtomicFactory::s_pInstance = NULL;

// -----------------------------------------------------------------------------------------------------------------

void CAtomicFactory::Release()					
{ 
    if (IsValid()) 
        delete s_pInstance; 
}

// -----------------------------------------------------------------------------------------------------------------

CAtomicFactory& CAtomicFactory::GetInstance()	
{ 
    Debug::Assert(IsValid(), "GetInstance() called on uninitialized singleton.");
    return *s_pInstance; 
}

// -----------------------------------------------------------------------------------------------------------------

bool CAtomicFactory::IsValid()
{
    return s_pInstance != NULL;
}

// -------------------------------------------------------------------------------

CAtomicFactory& CAtomicFactory::CreateInstance(IPlatformHandle& _rHandle)
{
	Release();
    
	switch (_rHandle.GetRenderType())
	{
#ifdef COMPILE_OPENGL
	case RenderType::OpenGL:
		s_pInstance = new OpenGL::CAtomicFactory(static_cast<COpenGLHandle&>(_rHandle));
		break;
#endif
			
#ifdef COMPILE_DX9
	case RenderType::DirectX9:
		s_pInstance = new DX9::CAtomicFactory(static_cast<CDirectXHandle<RenderType::DirectX9>&>(_rHandle));
		break;
#endif

#ifdef COMPILE_DX10
	case RenderType::DirectX10:
		s_pInstance = new DX10::CAtomicFactory(static_cast<CDirectXHandle<RenderType::DirectX10>&>(_rHandle));
		break;
#endif

#ifdef COMPILE_DX11
	case RenderType::DirectX11:
		s_pInstance = new DX11::CAtomicFactory(static_cast<CDirectXHandle<RenderType::DirectX11>&>(_rHandle));
		break;
#endif
	
	default:
        Debug::Error("CAtomicFactory::CreateInstance could not create a atomic factory.\n"
                     "Unkown RenderType or COMPILE_<render> macro not set");
        
		s_pInstance = NULL;
		break;
	}
    
    return *s_pInstance;
}


CB_SUBNAMESPACE_END